home *** CD-ROM | disk | FTP | other *** search
- Path: news.halcyon.com!usenet
- From: normanb@halcyon.com (Norm Bryar)
- Newsgroups: comp.lang.c++
- Subject: Re: C calling C++ function !!!
- Date: Sat, 24 Feb 1996 19:14:55 GMT
- Organization: Northwest Nexus Inc.
- Message-ID: <4gno2b$t1b@news.halcyon.com>
- References: <4ghldd$rt@news1.usa.pipeline.com>
- NNTP-Posting-Host: blv-pm11-ip3.halcyon.com
- X-Newsreader: Forte Free Agent 1.0.82
-
- grantp@usa.pipeline.com(Pete) wrote:
-
- >On Feb 21, 1996 12:24:45 in article <Re: C calling C++ function !!!>,
- >'a-kovalenko@star.nmb.ru (Alexey Kovalenko)' wrote:
- >
- >
- >>Hello, all.
- >>
- >>Few days ago Norm Bryar <normanb@halcyon.com> in his answer has written:
- >>
- >>>Something tells me it's not that simple.
- >>>
- >>[Skipped]
- >>>I believe the accepted thing to do is define a set of 'C' wrapper APIs
- >>>that you can export to your 'C' modules. Internally, your wrappers do
- >>>class method calls.
- >>
- >>It's wrong. Function in C++, defined as "C", just uses predefined C
- >calling
- >>convention instead compiler's preferred.
-
- First "It's wrong..." is misleading, since someone elided the bulk of
- my message.
- You sidestep name mangling with extern "C", clearly, though I don't
- know the effects on overloaded functions. But my mail also
- critiques someone's assertion that you could use ->vtabl or some other
- magic word to access the virtual function table inside a 'C' compiled
- module.
-
- >
- >What? Come again, please.
- >>
- >>Real solution here (don't say "It will not work" - I'm using this ):
- >>
- >Well, then you haven't posted all of the relevant code. Even
- >"filling the obvious blanks" will not produce workable code without
- >and extern "C" somewhere. In particular, the symbol Msg is defined
- >in the c++ module, which is referenced in the c code. C++ produces
- >a mangled version while the c code looks for _Msg. It won't link.
- >
- >>Situation : I'm working on project that contain both C and C++ sources and
- >I
- >>don't want to make C sources C++. I'm needing to call function-classmember
- >
- >>from C sources . Function must return char* by index(access to indexed
- >strings
- >>
- >>array, fuction in C used instead operator[] in C++).
- >>
- >>header contains:
- >>typedef char * (*MsgFunc)(int iNo );
- >>
- >>C++ file contains:
- >>
- >>#include "header.h"
- >>#include "cassdef.hpp"
- >>
- >>static Message * _Msg = new Message("messages.msg");
- >>static Message & Msg1 = *_Msg;
- >>// Two variables to avoid compiler warning about
- >>
- >>char * Msg( int iNo )
- >>{
- >>return Msg1[iNo]; // operator []
- >>}
-
- But for the missing extern "C" and missing instance parameter, this is
- what I meant: Define 'C' wrappers for the class method calls.
-
-
- >...
-
- We've also failed to mention the COM model used by Microsoft for OLE,
- the Win95 shell, and practically every other toolkit they're coming
- out with recently (Quartz, Audioman, etc.)? Look in objbase.h and
- you'll see an example of quite portable interface structure where no
- one cares what language either client or server was implemented in.
-
- --Norm
-
-